﻿using BCGM.MedicalWisdom.IServices.Orders;
using BCGM.MedicalWisdom.Model.Models;
using BCGM.MedicalWisdom.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using BCGM.MedicalWisdom.Model.ViewModels.Order;
using BCGM.MedicalWisdom.Controllers;
using Grpc.Core;
using Aop.Api.Domain;
using BCGM.MedicalWisdom.Common.HttpContextUser;
using ProtoBuf;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;

namespace BCGM.MedicalWisdom.Api.Controllers.Orders
{
    /// <summary>
    /// 药品分类管理模块
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ClassifyController : BaseApiController
    {
     
            public readonly IDrugclassifyServices _drugclassifyServices;
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="drugclassify"></param>

            public ClassifyController(IDrugclassifyServices drugclassify)
            {

                _drugclassifyServices = drugclassify;

            }
        /// <summary>
        /// /显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        //异步返回必须用task
        [HttpGet]
            public async Task<MessageModel<PageModel<Drugclassify>>> Get()
            {


                Expression<Func<Drugclassify, bool>> whereExpression = Drugclassify => Drugclassify.Pharmaceuticalsfenzhuang == false;

                return new MessageModel<PageModel<Drugclassify>>()
                {
                    msg = "获取成功",
                    success = true,
                    response = await _drugclassifyServices.QueryPage(whereExpression)
                };

            }
        /// <summary>
        /// 根据药品分类名查询表数据
        /// </summary>
        /// <param name="ClassIfyName"></param>
        /// <returns></returns>
         [HttpGet("{ClassIfyName}")]
               public MessageModel<List<DrugclassifyDto>> GetDrugCar(string ClassIfyName)
        {
                  return new MessageModel<List<DrugclassifyDto>>()
                  {
                    msg = "获取成功",
                    success = true,
                    response = _drugclassifyServices.QueryByname(ClassIfyName)
                };
            }

      /// <summary>
      /// 新增分类
      /// </summary>
      /// <param name="drugclassifyDto"></param>
      /// <returns></returns>
        [HttpPost]
        public MessageModel<bool> AddDrugCar([FromBody] DrugclassifyDto drugclassifyDto)
        {
            var id = _drugclassifyServices.AddDrugRedis(drugclassifyDto);
            return Success(id, "成功");
        }
        //图片上传
        //[HttpGet]
        //public void Filt() 
        //{
        //    //try catch 捕捉错误防止代码出错导致后面代码不能运行
        //    try
        //    {
        //        //判断头像的路径是否存在，如果返回TRUE用！让他不存在
        //        if (!System.IO.Directory.Exists(Server.MapPath("~/Document/ClassIfyPicture")))
        //        {
        //            //创建目录，指定他的路径
        //            System.IO.Directory.CreateDirectory(Server.MapPath("~/Document/ClassIfyPicture"));
        //        }
        //        //判断是否上传图片，ContentLength是文件长度，下列条件判断文件是否存在
        //        if (ClassIfyPicture != null && ClassIfyPicture.ContentLength>0)
        //        {
        //            //获取文件的扩展名称（根据文件的名称去拿到文件的后缀）
        //            string imgEtension = System.IO.Path.GetExtension(ClassIfyPicture.FileName);
        //            //拼接要保存的文件名称（文件名避免重复所以拼接一个新的文件名）
        //            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff")" "+Guid.NewGuid() + imgExtension;
        //            //拼接文件保存的路径
        //            tring filePath = Server.MapPath("~/Document/userPicture/") + fileName;
        //            //保存上传的文件到硬盘
        //            ClassIfyPicture.SaveAs(filePath);
        //            //文件名称保存到user对象,一般不会存完整的路径(也就是绝对路径)
        //            user.picture = fileName;

        //        }
        //    }
        //    catch (Exception e)
        //    {

        //        Console.WriteLine(e);
        //        msg.Text = "数据保存异常";
        //    }
        
        
        //}





        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// //FromBody 是将方法内的参数以json方式传递
        //[HttpPost]
        //public async Task<MessageModel<string>> Post([FromBody] Drugclassify request)
        //{
        //    var data = new MessageModel<string>();

        //    var id = await _drugclassifyServices.Add(request);
        //    data.success = id > 0;
        //    if (data.success)
        //    {
        //        data.response = id.ObjToString();
        //        data.msg = "添加成功";
        //    }

        //    return data;
        //}




    }
}
